package com.xywy.tangandroid.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.tangandroid.MyApplication;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.util.DeviceUtil;
import com.xywy.tangandroid.util.NetUtil;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.BaseRequest;
import com.xywy.tangandroid.vollery.VolleyManager;

public abstract class BaseActivity extends Activity implements OnClickListener {

	//
	public static int deviceWidth = 0;
	//
	public static int deviceHeight = 0;

	public static Context context;
	private ImageView left_img;
	protected  TextView middle_title;
	protected TextView right_title;
	private LinearLayout tobar;
	private ImageView right_img;
	protected ProgressDialog waittingDialog;
	private long mExitTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initBase();
		initView(savedInstanceState);
		initToBar();
		initListener();
		initData();
		MyApplication.getInstance().addActivity(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		VolleyManager.cancelRequest(context);
	}
	@Override
	protected void onStop() {
		super.onStop();
		VolleyManager.cancelRequest(context);
	}

	protected abstract void initView(Bundle savedInstanceState);

	protected abstract void initListener();

	protected abstract void initData();

	protected void initToBar() {
		tobar = (LinearLayout) findViewById(R.id.top_bar);
		if (tobar != null) {
			left_img = (ImageView) tobar.findViewById(R.id.left_img);
			middle_title = (TextView) tobar.findViewById(R.id.middle_title);
			right_title = (TextView) tobar.findViewById(R.id.right_title);
			right_img = (ImageView) tobar.findViewById(R.id.right_img);

			if (left_img != null)
				left_img.setOnClickListener(this);
			if (middle_title != null)
				middle_title.setOnClickListener(this);
			if (right_title != null)
				right_title.setOnClickListener(this);
			if (right_img != null)
				right_img.setOnClickListener(this);
		}

	}

	private void initBase() {
		context = this;

		if (deviceWidth == 0)
			deviceWidth = DeviceUtil.getDeviceWidth(this);
		if (deviceHeight == 0)
			deviceHeight = DeviceUtil.getDeviceHeight(this);

	}
	/**
	 * ���ñ�׼��ʽ��topbar����ʽ
	 * 
	 * @param left_id
	 *            ��߰�ťͼƬ����Դid��-1Ϊ����
	 * @param title
	 * @param right_id
	 *            �ұ߰�ťͼƬ����Դid��-1Ϊ����
	 * @param right_title
	 *            �ұߵı���
	 */
	protected void setTopbarOperation(int left_id, String title, int right_id) {
		if (left_img != null) {
			if (left_id == -1)
				left_img.setVisibility(View.INVISIBLE);
			else {
				left_img.setVisibility(View.VISIBLE);
				left_img.setImageResource(left_id);
			}
		}
		if (middle_title != null) {
			middle_title.setText(TextUtils.isEmpty(title) ? "" : title);
		}

		if (right_img != null) {
			if (right_id == -1)
				right_img.setVisibility(View.INVISIBLE);
			else {
				right_img.setVisibility(View.VISIBLE);
				right_img.setImageResource(right_id);
			}
		}
		// if(right_title!=null&&!TextUtils.isEmpty(righttitle)){
		// right_title.setVisibility(View.VISIBLE);
		// right_title.setText(TextUtils.isEmpty(righttitle)?"":righttitle);
		// }

	}

	public void waitingDialog(Context context, String message) {
		if (waittingDialog == null)
			waittingDialog = new ProgressDialog(context);
		waittingDialog.setCanceledOnTouchOutside(false);// �����಻��ʧ
		waittingDialog.setMessage(message);
		waittingDialog.show();
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	protected boolean executeRequest(BaseRequest<?> request) {
		if (NetUtil.hasNetwork(context)) {
			waitingDialog(this, getResources().getString(R.string.request));
			VolleyManager.addRequest(request, this);
			return true;
		} else {
			UIUtil.showToast(context, getResources().getString(R.string.not_network));
			UIUtil.DissDialog(this, waittingDialog);
			return false;
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}

	}
	

	
	public void showToast(String str){
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}

}
